package com.shangrila.biz.utils;

import static com.google.common.base.Preconditions.checkState;
import static org.apache.commons.lang.Validate.notEmpty;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author kalman03
 */
public class SpringBeanLoader implements ApplicationContextAware{

	private static ApplicationContext context;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringBeanLoader.context = applicationContext;
	}
	
	/**
	 * 获取和spring bean名称对象的bean实例<br>
	 * Return an instance, which may be shared or independent, of the specified bean. 
	 * @param beanName
	 * @return
	 */
    public static Object getSpringBean(String beanName) {
        notEmpty(beanName, "bean name is required");

        checkState(context != null, "spring application context is not injected");

        return context.getBean(beanName);
    }

	public static void setContext(ApplicationContext context) {
		SpringBeanLoader.context = context;
	}
    
}
